/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Server.SQL;

import Server.Helper.Analyzer;
import Server.MyDataType.MyDataDynamic;
import Server.lastproject.XMLManage;
import java.util.Vector;

/**
 *
 * @author Gnoc
 */

    /*
     * DELETE FROM table_name
     * WHERE some_column=some_value
     */
public class DeleteCommand extends MyCommand
{
    public DeleteCommand(String command)
    {
        super(command);
    }
    
    public MyDataDynamic Analysis(String cmd)
    {
        MyDataDynamic data = new MyDataDynamic();
        try
        {
            cmd = Analyzer.MakeCommandStandardization(cmd);
            String clause[] = SplitClause(cmd);

            if(clause[0].compareTo("") != 0)
                table = clause[0];

            // LastName='Tjessem' AND FirstName='Jakob'
            if(clause[1].compareTo("") != 0)
            {
                lstColumnsWhere = SplitClauseWhere(clause[1]);
            }
            boolean excute = Execute();
            if(excute)
                return data;
            else return null;
        }
        catch(Exception e)
        {
            return null;
        }
    }

    public boolean Execute()
    {
        try
        {
            XMLManage xml = new XMLManage();

            Vector<MyDataDynamic> lstData = new Vector<>();
            // Xóa không có đk
            if(lstColumnsWhere.size() <= 0)
            {
                MyDataDynamic lstDataTemp = new MyDataDynamic();
                lstDataTemp._vtRowData = xml.getAllRowData(table);
                lstDataTemp.set_vtNameColumn(xml.getAllNameColumn(table));
                lstData.add(lstDataTemp);
            }
            // Xóa có điều kiện
            if(lstColumnsWhere.size() > 0)
                lstData = ExcuteWhere();

            // Thực hiện xóa bỏ các row
            for(int i = 0; i<lstData.size(); i++)
            {
                for(int j = 0; j<lstData.get(i)._vtRowData.size(); j++)
                    xml.deleteDataRow(table, lstData.get(i)._vtRowData.get(j).ID);
            }

            xml.saveToFile();
        }
        catch(Exception ex)
        {
            return false;
        }
        
        return true;
    }

    /*
     * DELETE FROM table_name
     * WHERE some_column=some_value
     */
    public String[] SplitClause(String cmd) 
    {
        String[] clause = new String[2];
        clause[0] = "";
        clause[1] = "";
        
        String[] lstFirt = cmd.split(" ");
        
        int i = 0;
        //
        while(i<lstFirt.length && lstFirt[i].toUpperCase().compareTo("DELETE") != 0)
        {
            i ++;
        }
        
        i++;
        //
        while(i<lstFirt.length && lstFirt[i].toUpperCase().compareTo("FROM") != 0)
        {
            i ++;
        }
        
        i++;
        
        // Lấy table
        while(i<lstFirt.length && lstFirt[i].toUpperCase().compareTo("WHERE") != 0)
        {
            clause[0] = lstFirt[i];
            i ++;
        }
        
        i++;
        // Lấy WHERE
        while(i<lstFirt.length && lstFirt[i].toUpperCase().compareTo("WHERE") != 0)
        {
            clause[1] += lstFirt[i] + " ";
            i ++;
        }
        clause[0] = clause[0].trim();
        clause[1] = clause[1].trim();
        return clause;
    }

}
